package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.entity.Employee;
import com.sky.properties.Jwtproperties;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.util.JwtUtils;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("admin/employee")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private Jwtproperties Jwtproperties;
    @Autowired
    private Jwtproperties jwtproperties;

    /**
     * 员工登录
     * @param employeeLoginDTO
     * @return
     */
    @RequestMapping("/login")
    @ApiOperation("员工登录")
    public Result<EmployeeLoginVO> login(EmployeeLoginDTO employeeLoginDTO){
        log.info("员工登录");
        EmployeeLoginVO employeeLoginVO = employeeService.login(employeeLoginDTO);
        log.info("员工登录成功");
        //创建jwt 返回给前端
        Map<String, Object> claim = new HashMap();
        claim.put(JwtClaimsConstant.EMP_ID,employeeLoginVO.getId());
        String token = JwtUtils.createJwt(jwtproperties.getAdminSecretKey(), jwtproperties.getAdminTtl(), claim);
        employeeLoginVO.setToken(token);
        return Result.success(employeeLoginVO);
    }
}
